package org.springblade.modules.repair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.repair.dto.PartsWorkSaveDTO;
import org.springblade.modules.repair.dto.PartsWorkUpdateDTO;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.service.MaintainPartsWorkService;
import org.springblade.modules.repair.vo.*;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @Date 8:53 2022/6/29
 * @Description {
 *   部件更换接口控制器
 * }
 * @Author lsay
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/maintain/parts/work")
public class MaintainPartsWorkController {

	private final MaintainPartsWorkService partsWorkService;

	/**
	 * 配件申请-编辑-申请记录
	 * @param partsWorkSaveDTO 编辑申请记录
	 * @return 是否成功
	 */
	@PutMapping()
	@ApiOperation(value = "编辑")
	public R<Boolean> updatePartsWork(@RequestBody PartsWorkSaveDTO partsWorkSaveDTO){
		return R.status(partsWorkService.editPartsWork(partsWorkSaveDTO));
	}

	/**
	 * 配件申请-重新提交-申请记录
	 * @param partsWorkSaveDTO 编辑申请记录
	 * @return 是否成功
	 */
	@PostMapping("/resubmit")
	@ApiOperation(value = "重新提交")
	public R<Boolean> resubmit(@RequestBody PartsWorkSaveDTO partsWorkSaveDTO){
		return R.status(partsWorkService.resubmitPartsWork(partsWorkSaveDTO));
	}

	/**
	 * 部件更换工单完成
	 * @param partsWorkSaveDTO 申请记录
	 * @return 是否成功
	 */
	@PostMapping("/finish")
	public R<Boolean> finish(@RequestBody MaintainPlanContent partsWorkSaveDTO){
		return R.status(partsWorkService.finish(partsWorkSaveDTO));
	}

	/**
	 * 获取电梯部件更换记录
	 * @param pageQuery 查询条件
	 * @return IPage<ElevatorMalfunctionPartsVO> 电梯部件更换记录
	 */
	@GetMapping("/elevator/page")
	public R<IPage<ElevatorMalfunctionPartsVO>> selectElevatorPartsWorkPage(PartsWorksPageQuery pageQuery,Query query){
		return R.data(partsWorkService.selectElevatorPartsWorkPage(pageQuery, Condition.getPage(query)));
	}

	/**
	 * 获取用户申请的部件更换记录
	 * @param pageQuery 查询条件
	 * @return IPage<ElevatorMalfunctionPartsVO> 用户申请的部件更换记录
	 */
	@GetMapping("/user/page")
	public R<IPage<ElevatorMalfunctionPartsVO>> selectElevatorPartsWorkUserPage(PartsWorksPageQuery pageQuery,Query query){
		pageQuery.setUserId(AuthUtil.getUserId());
		return R.data(partsWorkService.selectElevatorPartsWorkPage(pageQuery, Condition.getPage(query)));
	}

	/**
	 * 获取用户审批的部件更换记录
	 * @param pageQuery 查询条件
	 * @param query 条件
	 * @return IPage<ElevatorMalfunctionPartsVO> 用户审批的部件更换记录
	 */
	@GetMapping("/audit/page")
	public R<HashMap<String,Object>> selectElevatorPartsWorkAuditPage(PartsWorksPageQuery pageQuery, Query query){
		return R.data(partsWorkService.selectElevatorPartsWorkAuditPage(pageQuery, Condition.getPage(query)));
	}


	/**
	 * 获取部件更换详情
	 * @param planId 工单id
	 * @return PartsWorkDetailVO 部件更换详情
	 */
	@GetMapping("/detail")
	public R<PartsWorkDetailVO> selectPartsWorkDetail(Long planId){
		return R.data(partsWorkService.selectPartsWorkDetail(planId));
	}

	@PostMapping("/partsSave")
	@ApiOperation(value = "新增配件工单")
	public R<Boolean> partsSave(@Valid @RequestBody PartsWorkSaveDTO partsWorkSave) {
		return R.status(partsWorkService.savePartsWork(partsWorkSave));
	}

	@GetMapping("/apply")
	@ApiOperation(value = "配件申请分页")
	public R<IPage<PartsApplyDetailVO>> partsApplyPage(@ApiIgnore @RequestParam Map<String, Object> param, Query query) {
		return R.data(partsWorkService.partsApplyPage(param, Condition.getPage(query)));
	}

	@PostMapping("/cancel/apply")
	@ApiOperation(value = "撤回申请")
	public R<Boolean> cancelApply(@ApiParam(value = "工单id", required = true) @RequestParam Long planId) {
		return R.data(partsWorkService.cancelApply(planId));
	}


}
